package com.svse.test;

import com.svse.entity.UserEntity;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author LiYuhang
 * @version 0.1
 * @application jdk8 Lambda表达式 集合过滤，排序，最大值，最小值，平均值等操作
 * @Copyright 上海危网信息科技有限公司版权所有
 * @company wwsoft
 * @Date 2020/1/2 10:38
 */

/**
 * List<Integer> integers = Arrays.asList(4, 5, 6,1, 2, 3,7, 8,8,9,10);
 *
 * List<Integer> evens = integers.stream().filter(i -> i % 2 == 0)
 *         .collect(Collectors.toList()); //过滤出偶数列表 [4,6,8,8,10]<br>
 * List<Integer> sortIntegers = integers.stream().sorted()
 *         .limit(5).collect(Collectors.toList());//排序并且提取出前5个元素 [1,2,3,4,5]
 *
 * List<Integer> squareList = integers.stream().map(i -> i * i).collect(Collectors.toList());//转成平方列表
 *
 * int sum = integers.stream().mapToInt(Integer::intValue).sum();//求和
 *
 * Set<Integer> integersSet = integers.stream().collect(Collectors.toSet());//转成其它数据结构比如set
 *
 * Map<Boolean, List<Integer>> listMap = integers.stream().collect(Collectors.groupingBy(i -> i % 2 == 0)); //根据奇偶性分组
 *
 * List<Integer> list = integers.stream().filter(i -> i % 2 == 0).map(i -> i * i).distinct().collect(Collectors.toList());//复合操作
 *
 */


public class Java8Lambda {
    /**
     * 功能描述 无参无返回值
     * @param list
     * @return void
     * @author LiYuhang
     * @date 2020/1/2
     */
    public  void lambdaWithParamAndNoReturn(List<UserEntity> list) {
        list.forEach(userBean -> System.out.println("hello," + userBean.getName()));
    }

    /**
     *功能描述 重新封装集合数据
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public   void initList(List<UserEntity> list){
        UserEntity userEntity=new UserEntity(1L,"ces",20,"xx");
        List<UserEntity> userList = list.stream().map(userBean ->
                new UserEntity(userBean.getId(),userBean.getName(),userBean.getAge(),userBean.getEmail())).collect(Collectors.toList());
        userList.forEach(userBean -> System.out.println("new list:" + userBean.getName()));
    }

    /**
     *功能描述 集合过滤
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */

    public  void filterList(List<UserEntity> list){
        //name包含g的所有属性
        //List<UserBean> filterList = list.stream().filter(userBean -> userBean.getName().contains("g")).collect(Collectors.toList());
        //id>6的所有集合
        List<UserEntity> filterList = list.stream().filter(userBean -> userBean.getId() > 6L).collect(Collectors.toList());
        filterList.forEach(userBean -> System.out.println("filter list:" + userBean.getName()));
    }


    /**
     *功能描述 排序
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public static void sortList(List<UserEntity> list){
        //按userid排序
        List<UserEntity> sortList = list.stream().sorted(Comparator.comparing(UserEntity::getId)).collect(Collectors.toList());
        sortList.forEach(userBean -> System.out.println("sortList:" + userBean.getId() + "," + userBean.getId()));
    }

    /**
     *功能描述 多条件排序
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void multiSortList(List<UserEntity> list){
        list.sort(Comparator.comparing(UserEntity::getId).thenComparing(UserEntity::getName));
        list.forEach(userBean -> System.out.println("multiSortList:" + userBean.getId() + "," + userBean.getName()));
    }
    /**
     *功能描述 倒序
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void reversedSortList(List<UserEntity> list){
        //第一种写法
        Comparator<UserEntity> comparator = Comparator.comparing(UserEntity::getName);
        //倒叙
        list.sort(comparator.reversed());
        //第二种写法
        list.sort(Comparator.comparing(UserEntity::getId).reversed());

        list.forEach(userBean -> System.out.println("reverseSortList:" + userBean.getId() + "," + userBean.getName()));
    }
    /**
     *功能描述 多条件倒序
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void multiReversedSortList(List<UserEntity> list){
        list.sort(Comparator.comparing(UserEntity::getId).thenComparing(UserEntity::getName).reversed());
        list.forEach(userBean -> System.out.println("multiReversedSortList:" + userBean.getId() + "," + userBean.getName()));
    }
    /**
     *功能描述 集合分组
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void groupByList(List<UserEntity> list) {
        Map<String, List<UserEntity>> groupByMap = list.stream().collect(Collectors.groupingBy(UserEntity::getName));
        groupByMap.forEach((k, v) -> System.out.println(k + "," + v));
    }
    /**k
     *功能描述 求和
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void sumByList(List<UserEntity> list){
        System.out.println("sum="+ list.stream().mapToInt(UserEntity::getAge).sum());
    }

    /**
     *功能描述 最大值
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void maxByList(List<UserEntity> list){
        OptionalInt optional = list.stream().mapToInt(UserEntity::getAge).max();
        System.out.println("max=" + optional.getAsInt());
    }
    /**
     *功能描述 最小值
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public static void minByList(List<UserEntity> list){
        OptionalInt optional = list.stream().mapToInt(UserEntity::getAge).min();
        System.out.println("min=" + optional.getAsInt());
    }
    /**
     *功能描述 平均值
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void averageByList(List<UserEntity> list){
        OptionalDouble optionalDouble = list.stream().mapToInt(UserEntity::getAge).average();
        System.out.println("average=" + optionalDouble.getAsDouble());
    }

    /**
     *功能描述 List转map
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void listToMap(List<UserEntity> list){
        //用 (k1,k2)->k1 来设置，如果有重复的key,则保留key1,舍弃key2
        Map<Long,UserEntity> map = list.stream().collect(Collectors.toMap(UserEntity::getId,userBean -> userBean, (k1, k2) -> k1));
        map.forEach((k,v) ->
                System.out.println("k=" + k + ",v=" + v)
        );

        map.forEach((key, value) -> {
            System.out.println(key + "：" + value);
        });
    }

    /**
     *功能描述 map转list
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void mapToList(Map<Long,String> map){
        List<UserEntity> list = map.entrySet().stream().sorted(
                Comparator.comparing(key -> key.getKey())).map(
                        key -> new UserEntity(key.getKey(),"",null,""))
                .collect(Collectors.toList());
        list.forEach(userBean -> System.out.println(userBean.getId() + "," + userBean.getName()));
    }

    /**
     *功能描述 字符串转list
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void stringToList(String str){
        //不需要处理
         List<String> list1 = Arrays.asList(str.split(","));
        //需要处理
        List<String> list = Arrays.asList(str.split(",")).stream().map(string -> String.valueOf(string)).collect(Collectors.toList());
        list.forEach(string -> System.out.println(string));
    }

    /**
     *功能描述 姓名以逗号拼接
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void joinStringValueByList(List<UserEntity> list){
        System.out.println(list.stream().map(UserEntity::getName).collect(Collectors.joining(",")));
    }

    /**
     *功能描述 分组统计
     * @author LiYuhang
     * @date 2020/1/2
     * @return void
     */
    public  void countByList(List<UserEntity> list){
        Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(UserEntity::getAge,Collectors.counting()));
        map.forEach((k,v) -> System.out.println("key=" + k + ",value=" + v));
    }
    @Test
    public  void test() {
        //List<UserBean> list = Arrays.asList(new UserBean(1, "AKB001", "LiYuhang"), new UserBean("2", "AKB002", "apple"), new UserBean("3", "AKB003", "cat"));
        List<UserEntity> list = Stream.of(new UserEntity(1L, "AKB001", 10,"LiYuhang"),
                new UserEntity(2L, "AKB002", 11,"apple"),
                new UserEntity(4L, "AKB004", 5,"dog"),
                new UserEntity(5L, "AKB005",5, "egg"),
                new UserEntity(6L, "AKB006", 4,"frog"),
                new UserEntity(6L, "AKB006",4, "banana"),
                new UserEntity(7L, "AKB007",10, "google"),
                new UserEntity(3L, "AKB003",11, "cat"))
                .collect(Collectors.toList());
        lambdaWithParamAndNoReturn(list);
        initList(list);
        filterList(list);
        sortList(list);
        reversedSortList(list);
        multiSortList(list);
        multiReversedSortList(list);
        groupByList(list);
     /*   sumByList(list);
        maxByList(list);
        minByList(list);
        averageByList(list);
        listToMap(list);
        String str = "apple,banana,cat,dog";
        stringToList(str);
        Map<Long,String> map =new HashMap<Long, String>() {
            {
                put(1L, "apple");
                put(2L, "banana");
                put(3L, "cat");
                put(4L, "dog");
                put(5L, "frog");
            }
        };
        mapToList(map);
        joinStringValueByList(list);
        countByList(list);*/
    }


}
